package com.xjnt.base.support.admin.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.xjnt.base.support.admin.entity.Account;
import com.xjnt.base.support.admin.entity.Group;
import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.base.support.admin.entity.Role;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.sql.SqlKit;
import com.xjnt.frameworks.plugin.sql.criterion.Restrictions;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;

/**
 * @author caoyong
 *
 */
@Service
public class RoleService extends BaseService<Role> {

	@Override
	public Role getModel() {
		return Role.me;
	}
	
	public boolean allocPermission(String roleId, String permId){
		int k = Db.update(SqlKit.findSql("Role.relationRoleAndPermission"), roleId, permId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public void allocPermission(String roleId, List<Permission> permissions){
		for (Permission permission : permissions) {
			Db.update(SqlKit.findSql("Role.relationRoleAndPermission"), roleId, permission.getStr(Permission.PK_ID));
		}
	}
	
	public boolean removePermission(String roleId, String permId){
		int k = Db.update(SqlKit.findSql("Role.removeRoleAndPermission"), roleId, permId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public void removePermissions(String roleId, List<Permission> permissions){
		for (Permission permission : permissions) {
			Db.update(SqlKit.findSql("Role.removeRoleAndPermission"), roleId, permission.getStr(Permission.PK_ID));
		}
	}
	
	public boolean removeAllPermission(String roleId){
		int k = Db.update(SqlKit.findSql("Role.removeRoleAndAllPermission"), roleId);
		if(k > 0){
			return true;
		}
		return false;
	}

	public List<Role> findByIds(Object[] ids) {
		SqlCriteria criteria = new SqlCriteria(getModel());
		criteria.add(Restrictions.in(Role.PK_ID, ids));
		return getModel().find(criteria.toSql(), ids);
	}
	
	public List<Role> findByAccountId(String accountId) {
		SqlCriteria criteria = new SqlCriteria(getModel(), "role");
		criteria.addRelation("sys_account_role", "sar");
		criteria.addRelation(Account.me, "acc");
		criteria.add(Restrictions.eqProp("role.uuid", "sar.roleid"));
		criteria.add(Restrictions.eqProp("acc.uuid", "sar.accountid"));
		criteria.add(Restrictions.eq("acc.uuid", accountId));
		return getModel().find(criteria.toSql(), accountId);
	}
	
	public List<Role> findByGroupId(String groupId) {
		SqlCriteria criteria = new SqlCriteria(getModel(), "role");
		criteria.addRelation("sys_group_role", "sgr");
		criteria.addRelation(Group.me, "sg");
		criteria.add(Restrictions.eqProp("role.uuid", "sgr.roleid"));
		criteria.add(Restrictions.eqProp("sg.uuid", "sgr.groupid"));
		criteria.add(Restrictions.eq("sg.uuid", groupId));
		return getModel().find(criteria.toSql(), groupId);
	}

	public List<Role> findByGroupIds(Object[] groupIds) {
		SqlCriteria criteria = new SqlCriteria(getModel(), "role");
		criteria.addRelation("sys_group_role", "sgr");
		criteria.addRelation(Group.me, "sg");
		criteria.add(Restrictions.eqProp("role.uuid", "sgr.roleid"));
		criteria.add(Restrictions.eqProp("sg.uuid", "sgr.groupid"));
		criteria.add(Restrictions.in("sg.uuid", groupIds));
		return getModel().find(criteria.toSql(), groupIds);
	}
}
